package com.zzyl.controller;

import com.github.pagehelper.Page;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

/**
 * @author Jun
 */
@RestController
@RequestMapping("/alert-rule")
@Api(tags = "告警规则管理")
@Slf4j
public class AlertRuleController {

    @Autowired
    private AlertRuleService alertRuleService;

    @PostMapping("/create")
    @ApiOperation("新增告警规则")
    public ResponseResult create(@RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.create(alertRuleDto);
        return success();
    }

    @GetMapping("/get-page")
    @ApiOperation("分页查询告警规则")
    public ResponseResult<AlertRuleVo> getPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "alertRuleName", required = false) String alertRuleName,
                                               @RequestParam(value = "functionName", required = false) String functionName,
                                               @RequestParam(value = "productKey", required = false) String productKey
    ){
        PageResponse<AlertRuleVo> alertRuleVos = alertRuleService.getPage(pageNum,pageSize,alertRuleName,functionName,productKey);
        return success(alertRuleVos);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除告警规则")
    public ResponseResult delete(@PathVariable("id") Long id){
        alertRuleService.delete(id);
        return success();
    }

    @ApiOperation(value = "根据id查询告警规则")
    @GetMapping("/read/{id}")
    public ResponseResult<AlertRuleVo> read(@PathVariable Long id){
        AlertRuleVo alertRuleVo = alertRuleService.read(id);
        return ResponseResult.success(alertRuleVo);
    }
    @ApiOperation(value = "根据id更新告警规则")
    @PutMapping("/update/{id}")
    public ResponseResult update(@RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.update(alertRuleDto);
        return ResponseResult.success();
    }
    @ApiOperation(value = "启用禁用")
    @PutMapping("/{id}/status/{status}")
    public ResponseResult updateStatus(@PathVariable Integer id, @PathVariable Integer status){
        alertRuleService.updateStatus(id, status);
        return ResponseResult.success();
    }
}
